package net.ccbluex.liquidbounce.ui.client.hud.element.elements

import me.utils.render.RoundedUtil
import me.utils.render.ShadowUtils
import net.ccbluex.liquidbounce.LiquidBounce
import net.ccbluex.liquidbounce.features.module.modules.color.ColorMixer
import net.ccbluex.liquidbounce.features.module.modules.render.CnModulename
import net.ccbluex.liquidbounce.features.module.modules.render.HUD
import net.ccbluex.liquidbounce.features.module.modules.render.RenderPosHelper
import net.ccbluex.liquidbounce.font.CFontLoad
import net.ccbluex.liquidbounce.ui.client.hud.element.Border
import net.ccbluex.liquidbounce.ui.client.hud.element.Element
import net.ccbluex.liquidbounce.ui.client.hud.element.ElementInfo
import net.ccbluex.liquidbounce.utils.misc.RandomUtils.easinghealth
import net.ccbluex.liquidbounce.utils.misc.RandomUtils.updatebackground
import net.ccbluex.liquidbounce.utils.render.RenderUtils
import net.ccbluex.liquidbounce.value.BoolValue
import net.minecraft.client.renderer.GlStateManager
import org.lwjgl.opengl.GL11
import java.awt.Color

@ElementInfo(name = "KeyBinds")
class KeyBinds : Element() {
    val onlyState = BoolValue("OnlyModuleState", true)
    private val shadow = BoolValue("Shadow", true)

    var blurmodule = LiquidBounce.moduleManager.getModule(HUD::class.java) as HUD
    var renpos = LiquidBounce.moduleManager.getModule(RenderPosHelper::class.java) as RenderPosHelper
    var cnModulename = LiquidBounce.moduleManager.getModule(CnModulename::class.java) as CnModulename
    override fun drawElement(): Border? {
        var y2 = 0
        var y3 = 0
        //draw Background   打印背景
        RoundedUtil.drawRound(0f, 0f, 120f, 15f, 2.5f, Color(0, 0, 0, 200))
        RoundedUtil.drawRound(0f, 0f, 120f, 15 + easinghealth, 2.5f, Color(0, 0, 0, 170))
        //贴一些阴影
        //drawShadow
        if (shadow.get()){
            GL11.glTranslated(-renderX, -renderY, 0.0)
            GL11.glScalef(1F, 1F, 1F)
            GL11.glPushMatrix()
            ShadowUtils.shadow(8f, {
                GL11.glPushMatrix()
                GL11.glTranslated(renderX, renderY, 0.0)
                GL11.glScalef(scale, scale, scale)
                RenderUtils.originalRoundedRect(
                    -0.7f,
                    -0.8f,120f,15 + easinghealth,
                    2.0f,Color(0,0,0,210).rgb)


                GL11.glPopMatrix()
            }, {

                GL11.glPushMatrix()
                GL11.glTranslated(renderX, renderY, 0.0)
                GL11.glScalef(scale, scale, scale)
                GlStateManager.enableBlend()
                GlStateManager.disableTexture2D()
                GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0)

                RenderUtils.originalRoundedRect(
                    -0.7f,
                    -0.8f,120f,15 + easinghealth,
                   2.0f,Color(blurmodule.shadowred.get(),blurmodule.shadowgreen.get(),blurmodule.shadowblue.get(),blurmodule.shadowalpha.get()).rgb)
                GlStateManager.enableTexture2D()
                GlStateManager.disableBlend()
                GL11.glPopMatrix()
            })
            GL11.glPopMatrix()
            GL11.glScalef(scale, scale, scale)
            GL11.glTranslated(renderX, renderY, 0.0)
        }

        //draw Title
        //打印一些字符串
        CFontLoad.for18.drawStringWithShadow("       KeyBinds", 23.0, 6.0, -1)
        CFontLoad.forico24.drawStringWithShadow("E", 5.0, 3.5, ColorMixer.getMixedColor(-1 * 2 * 10, 5).rgb)
        //draw Module Bind
        for (module in LiquidBounce.moduleManager.modules) {
            if (module.keyBind == 0) continue

            if (!module.state) continue
            if (cnModulename.state){
                mc.fontRendererObj.drawStringWithShadow(module.name, 5, (y2 + 21f).toInt(), -1)

            }         else{
                CFontLoad.for18.drawStringWithShadow(module.name, 5.0, (y2 + 21f).toDouble(), -1)
            }
            if (cnModulename.state){
                mc.fontRendererObj.drawStringWithShadow(module.name, 5, (y2 + 21f).toInt(), -1)

            }         else{
                CFontLoad.for18.drawString(
                    "[Toggle]",
                    (115 - CFontLoad.for18.getStringWidth("[Toggle]")).toFloat().toDouble(),
                    (y2 + 21f).toDouble(),
                    if (module.state) Color(39, 196, 11).rgb else Color(232, 19, 19).rgb,
                    false
                )
            }
            //CFontLoad.forico22.drawStringWithShadow(drawmoduleico(module.getName()), 65, y2 + 21f,-1);
            //CFontLoad.for17.drawStringWithShadow("[Toggle]", 100 - CFontLoad.newforever17.getStringWidth("[Toggle]"), y2 + 21f, module.getState() ? new Color(255, 255, 255).getRGB() : new Color(100,100,100).getRGB());
            y2 += 12
            y3 += 13f.toInt()
        }
        updatebackground(y3.toFloat())
        return Border(0f, 0f, 84f, (17 + getmoduley()).toFloat())
    }

    fun getmoduley(): Int {
        var y = 0
        for (module in LiquidBounce.moduleManager.modules) {
            if (module.keyBind == 0) continue
            if (onlyState.get()) {
                if (!module.state) continue
            }
            y += 12
        }
        return y
    }
}
